from datetime import datetime

from django.db import models
from user.models import User, Addr


# Create your models here.
class Kinds(models.Model):
    cate_name=models.CharField(max_length=50,verbose_name='类别名')
    img=models.CharField(max_length=200,verbose_name='类别图片')
    create_time = models.DateTimeField(verbose_name='创建时间', default=datetime.now)
    update_time = models.DateTimeField(verbose_name='修改时间', default=datetime.now)
    class Meta:
        db_table='kinds'
        verbose_name_plural='类别'
    def __str__(self):
        return self.cate_name
class Merchant(models.Model):
    mobile = models.CharField(max_length=11, verbose_name='手机号')
    name = models.CharField(max_length=30, verbose_name='店铺名称名')
    password = models.CharField(max_length=256, verbose_name='密码')
    avatar = models.CharField(max_length=256, default='', verbose_name='头像')
    business_status = models.IntegerField(default=True, verbose_name='状态：True：营业，False：停业')
    start_send=models.IntegerField(default=0,verbose_name='起送价格')
    sending=models.IntegerField(default=0,verbose_name='配送价格')
    average=models.IntegerField(default=0,verbose_name='人均消费')
    business_time = models.CharField(default='', max_length=30, verbose_name='营业时间')
    kind=models.ForeignKey(Kinds,on_delete=models.CASCADE,verbose_name='类别')
    create_time = models.DateTimeField(verbose_name='注册时间', default=datetime.now)
    last_login = models.DateTimeField(verbose_name='最后登录时间', default=datetime.now )
    class Meta:
        db_table='merchant'
        verbose_name_plural='商家表'
    def __str__(self):
        return self.name
class Category(models.Model):
    cate_name=models.CharField(max_length=50,verbose_name='类别名')
    merchant = models.ForeignKey(Merchant, default=1, on_delete=models.CASCADE, verbose_name='所属商家')
    create_time = models.DateTimeField(verbose_name='创建时间', default=datetime.now)
    update_time = models.DateTimeField(verbose_name='修改时间', default=datetime.now)
    class Meta:
        db_table='cate'
        verbose_name_plural='商品类别'
class Goods(models.Model):
    sku_name=models.CharField(max_length=100,verbose_name='商品名')
    price=models.DecimalField(max_digits=13,decimal_places=2,verbose_name='商品价格')
    selling_price=models.DecimalField(max_digits=13,decimal_places=2,verbose_name='商品销售价格')
    img=models.CharField(max_length=200,verbose_name='商品默认图片')
    instruction=models.TextField(verbose_name='商品描述信息')
    count=models.IntegerField(verbose_name='商品销售数量',default=0)
    stock=models.IntegerField(verbose_name='商品库存数量',default=0)
    cate=models.ForeignKey(Category,on_delete=models.CASCADE,verbose_name='所属类别')
    merchant=models.ForeignKey(Merchant,on_delete=models.CASCADE,verbose_name='所属商家')
    online=models.BooleanField('是否在售',default=True)
    create_time = models.DateTimeField(verbose_name='创建时间', default=datetime.now)
    update_time = models.DateTimeField(verbose_name='修改时间', default=datetime.now)
    class Meta:
        db_table='goods'
        verbose_name_plural='商品信息'
    def __str__(self):
        return self.sku_name

class MerchantCollect(models.Model):
    user=models.ForeignKey(User,on_delete=models.CASCADE,verbose_name='用户')
    merchant=models.ForeignKey(Merchant,on_delete=models.CASCADE,verbose_name='商品')
    create_time = models.DateTimeField(verbose_name='创建时间', default=datetime.now)
    update_time = models.DateTimeField(verbose_name='修改时间', default=datetime.now)
    class Meta:
        verbose_name_plural='收藏表'
        db_table='good_collect'
    def __str__(self):
        return f'{self.user.username}:{self.merchant.name}'
class Order(models.Model):
    status_chioces=((0, '待支付'), (1, '待发货'), (2, '待收货'), (3, '待评价'), (4, '已完成'))
    order_id = models.CharField(max_length=100, primary_key=True, verbose_name='订单号')
    user = models.ForeignKey(User, on_delete=models.CASCADE,verbose_name='用户')  # related_name用于反向查询 user.order.all()
    address = models.ForeignKey(Addr, on_delete=models.CASCADE, verbose_name='收货地址')
    total_amount = models.DecimalField(max_digits=9, decimal_places=2,default=0, verbose_name='总价')
    freight = models.DecimalField(max_digits=7, decimal_places=2, verbose_name='运费')
    total_count = models.IntegerField(verbose_name='商品总数', default=0)
    pay_method = models.CharField(max_length=10, choices=((1, '支付宝'), (2, '银联')), verbose_name='支付方式')
    pay_status = models.SmallIntegerField(choices=status_chioces,verbose_name='支付状态')
    created_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    def __str__(self):
        return "订单:%s" % self.order_id
    class Meta:
        db_table = 'order_tb'
        verbose_name = '订单表'
        verbose_name_plural = verbose_name

class OrderGoods(models.Model):
    """订单商品"""
    good = models.ForeignKey(Goods, on_delete=models.CASCADE, verbose_name="订单商品")
    count = models.IntegerField(default=1, verbose_name="数量")
    price = models.DecimalField(max_digits=7, decimal_places=2, verbose_name="单价")
    order = models.ForeignKey(Order, related_name='orderGood', on_delete=models.CASCADE, verbose_name="订单")
    created_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        db_table = "order_goods_table"
        verbose_name = '订单商品表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f"订单{self.order.order_id}中的商品{self.good.sku_name}"
class Comment(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='用户')
    good = models.ForeignKey(Goods, on_delete=models.CASCADE, verbose_name="商品")
    content=models.CharField(max_length=50,verbose_name='评论')
    SCORE_CHOICES = ((0, '0分'), (1, '20分'), (2, '40分'), (3, '60分'), (4, '80分'), (5, '100分'))
    score = models.SmallIntegerField(choices=SCORE_CHOICES, default=5, verbose_name='满意度评分')
    created_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    class Meta:
        db_table='comment'
        verbose_name_plural='评论表'
    def __str__(self):
        return self.content
